package com.usian.admin.service.impl;

import com.usian.admin.feign.ArticleFeign;
import com.usian.admin.feign.WemediaFeign;
import com.usian.admin.service.AdChannelService;
import com.usian.admin.service.NewsAuthService;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewsAuthServiceImpl implements NewsAuthService {

    @Autowired
    private WemediaFeign wemediaFeign;

    @Value("${fastdfs.url}")
    private String fastServiceUrl;

    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private AdChannelService adChannelService;

    @Override
    public ResponseResult findList(NewsAuthDto authDto) {
        ResponseResult responseResult = wemediaFeign.findList(authDto);
        responseResult.setHost(fastServiceUrl);
        return responseResult;
    }

    @Override
    public ResponseResult findOne(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult responseResult=wemediaFeign.findOne(id);
        responseResult.setHost(fastServiceUrl);
        return null;
    }

    @Override
    public ResponseResult newsAuth(NewsAuthDto newsAuthDto, int type) {
        if (newsAuthDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmNews wmNews = new WmNews();
        wmNews.setId(newsAuthDto.getId());
        if (type==1){
            //updateWmNews(wmNews,WmNews.Status.ADMIN_SUCCESS.getCode(),"审核通过！");
            wmNews = wemediaFeign.findNewsById(newsAuthDto.getId());
            saveApArticle(wmNews);


        }else{

            updateWmNews(wmNews,WmNews.Status.FAIL.getCode(),newsAuthDto.getMsg());

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    private void updateWmNews(WmNews wmNews, Short status, String reason){
        wmNews.setStatus(status);
        wmNews.setReason(reason);
        wemediaFeign.updateNewsStatus(wmNews);
    }

    private void saveApArticle(WmNews wmNews){
        ApArticle apArticle = saveArticle(wmNews);

        saveApArticleConfig(apArticle.getId());

        saveApArticleContent(apArticle.getId(),wmNews.getContent());
        wmNews.setArticleId(apArticle.getId());
        updateWmNews(wmNews,WmNews.Status.PUBLISHED.getCode(),"文章审核通过，已发布");
    }

    private void saveApArticleContent(Long articelId, String content) {
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(articelId);
        apArticleContent.setContent(content);
        articleFeign.saveArticleContent(apArticleContent);
    }

    private void saveApArticleConfig(Long articelId) {
        ApArticleConfig apArticleConfig=new ApArticleConfig();
        apArticleConfig.setArticleId(articelId);
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsComment(false);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsForward(true);
        articleFeign.saveArticleConfig(apArticleConfig);
    }



    //保存ap/_article
    private ApArticle saveArticle(WmNews wmNews) {
        ApArticle apArticle=new ApArticle();

        apArticle.setTitle(wmNews.getTitle());
        WmUser wmUser=wemediaFeign.findUserById(wmNews.getUserId());
        ApAuthor apAuthor=articleFeign.findByName(wmUser.getName());
        if (apAuthor!=null){
            apArticle.setAuthorId(apAuthor.getId().longValue());
            apArticle.setAuthorName(apAuthor.getName());
        }

        AdChannel adChannel=adChannelService.getById(wmNews.getChannelId());

        if (adChannel!=null){
            apArticle.setChannelId(adChannel.getId());
            apArticle.setAuthorName(adChannel.getName());
        }
        apArticle.setLayout(wmNews.getType());
        apArticle.setImages(wmNews.getImages());
        apArticle.setLabels(wmNews.getLabels());
        apArticle.setCreatedTime(new Date());
        apArticle.setPublishTime(new Date());

        apArticle = articleFeign.saveArticle(apArticle);
        return apArticle;

    }
}
